package com.hjh.vip.demo.action;


import com.hjh.vip.demo.service.IModifyService;
import com.hjh.vip.demo.service.IQueryService;
import com.hjh.vip.spring.framework.v2.annotation.HjhAutowired;
import com.hjh.vip.spring.framework.v2.annotation.HjhController;
import com.hjh.vip.spring.framework.v2.annotation.HjhRequestMapping;
import com.hjh.vip.spring.framework.v2.annotation.HjhRequestParam;
import com.hjh.vip.spring.framework.v2.servlet.HjhModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 公布接口url
 *
 * @author Tom
 */
@HjhController
@HjhRequestMapping("/web")
public class MyAction {

    @HjhAutowired
    private IQueryService queryService;
    @HjhAutowired
    IModifyService modifyService;

    @HjhRequestMapping("/query.json")
    public HjhModelAndView query(HttpServletRequest request, HttpServletResponse response,
                                 @HjhRequestParam("name") String name) {
        String result = queryService.query(name);
        return out(response, result);
    }

    @HjhRequestMapping("/add*.json")
    public HjhModelAndView add(HttpServletRequest request, HttpServletResponse response,
                               @HjhRequestParam("name") String name, @HjhRequestParam("addr") String addr) {
        String result = modifyService.add(name, addr);
        return out(response, result);
    }

    @HjhRequestMapping("/remove.json")
    public HjhModelAndView remove(HttpServletRequest request, HttpServletResponse response,
                                  @HjhRequestParam("id") Integer id) {
        String result = modifyService.remove(id);
        return out(response, result);
    }

    @HjhRequestMapping("/edit.json")
    public HjhModelAndView edit(HttpServletRequest request, HttpServletResponse response,
                                @HjhRequestParam("id") Integer id,
                                @HjhRequestParam("name") String name) {
        String result = modifyService.edit(id, name);
        return out(response, result);
    }


    private HjhModelAndView out(HttpServletResponse resp, String str) {
        try {
            resp.getWriter().write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
